package com.qingmi.farmer.ui.demand;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.qingmi.farmer.BR;
import com.qingmi.farmer.R;
import com.qingmi.farmer.data.ActionsCode;
import com.qingmi.farmer.data.UserData;
import com.qingmi.farmer.data.model.Buyer;
import com.qingmi.farmer.data.model.GrainDemand;
import com.qingmi.farmer.data.model.GrainLevel;
import com.qingmi.farmer.data.model.GrainType;
import com.qingmi.farmer.data.repository.data.GrainDataRepository;
import com.qingmi.farmer.data.request.GetGrainDemandListReq;
import com.qingmi.farmer.databinding.ActivityDemandListBinding;
import com.wjjun.base.ui.base.ListActivity;
import com.qingmi.farmer.viewmodel.GrainDemandItemViewModel;
import com.wjjun.basemvpvm.data.exception.MyException;
import com.wjjun.basemvpvm.data.observer.DefaultObserver;
import com.wjjun.basemvpvm.viewmodel.BaseViewModel;
import com.wjjun.customview.views.dropdownmenu.OnItemMenuClickListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import me.tatarka.bindingcollectionadapter2.ItemBinding;


/**
 * 收购市场
 */
public class DemandListActivity extends ListActivity<DemandListViewModel, ActivityDemandListBinding> {

    public static void start(Context context) {
        Intent intent = new Intent(context, DemandListActivity.class);
        context.startActivity(intent);
    }

    public static void start(Context context, boolean showRight) {
        Intent intent = new Intent(context, DemandListActivity.class);
        intent.putExtra(ActionsCode.DATA_SHOW_RIGTH, showRight);
        context.startActivity(intent);
    }


    @Inject
    GrainDataRepository grainDataRepository;

    @Inject
    UserData userData;

    private String typeId;

    private String levelId;

    private String buyerId;

    @Override
    public int getLayoutId() {
        return R.layout.activity_demand_list;
    }

    @Override
    protected DemandListViewModel initViewModel() {
        return new DemandListViewModel();
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void initContent() {
        super.initContent();
        grainDataRepository.bind(this);

        viewModel.setTitle(getString(R.string.title_demand));

        if (getIntent().getBooleanExtra(ActionsCode.DATA_SHOW_RIGTH, true) &&
                userData.isJoinCooperative()) {
            viewModel.addRightText(getString(R.string.txt_to_sell_grain));
        }
        viewModel.menuAdapter.set(new DemandMenuAdapter(this));
    }

    @Override
    protected void initData() {
        grainDataRepository.getGrainTypeList(new DefaultObserver<List<GrainType>>() {
            @Override
            public void onNext(List<GrainType> grainTypes) {
                if (grainTypes != null && !grainTypes.isEmpty()) {
                    for (GrainType item : grainTypes) {
                        if (item.getChildParameter() == null) {
                            item.setChildParameter(new ArrayList<GrainType>());
                        }
                        item.getChildParameter().add(0, new GrainType(item.getId(), getString(R.string.format_all, item.getName())));
                    }
                }
                viewModel.menuAdapter.get().setTypeList(grainTypes);
            }

            @Override
            public void onError(MyException e) {
                showToast(e.getMessage());
            }
        });

        grainDataRepository.getGrainLevelList(new DefaultObserver<List<GrainLevel>>() {
            @Override
            public void onNext(List<GrainLevel> grainLevels) {
                viewModel.menuAdapter.get().setLevelList(grainLevels);
            }

            @Override
            public void onError(MyException e) {
                showToast(e.getMessage());
            }
        });

        grainDataRepository.getBuyerList(new DefaultObserver<List<Buyer>>() {
            @Override
            public void onNext(List<Buyer> parameters) {
                viewModel.menuAdapter.get().setBuyerList(parameters);
            }

            @Override
            public void onError(MyException e) {
                showToast(e.getMessage());
            }
        });

        super.initData();
    }

    @Override
    public void onListItemBind(ItemBinding itemBinding, int position, Object item) {
        if (item instanceof GrainDemandItemViewModel) {   //收购
            itemBinding.set(BR.viewModel, R.layout.layout_item_grain_demand).bindExtra(BR.presenter, viewGroupPresenter);
        }
    }

    @Override
    protected void onListItemClick(View view, BaseViewModel item) {
        super.onListItemClick(view, item);
        if (item instanceof GrainDemandItemViewModel) {
            DemandDetailActivity.start(DemandListActivity.this, ((GrainDemandItemViewModel) item).id);
        }
    }

    public final OnItemMenuClickListener itemMenuClickListener = new OnItemMenuClickListener() {
        @SuppressWarnings("ConstantConditions")
        @Override
        public void onMenuItemClick(int index) {
        }

        @Override
        public void onContentItemClick(int index, Object item1, Object item2) {
            if (index == 0) {   //品种
                if (item2 != null) {
                    typeId = (String) item2;
                } else {
                    typeId = (String) item1;
                }
            } else if (index == 1) {    //等级
                levelId = (String) item1;
            } else if (index == 3) {  //收购商
                buyerId = (String) item1;
            }
            autoRefresh();
        }
    };

    @Override
    public void refreshData(boolean init) {
        super.refreshData(init);
        GetGrainDemandListReq req = new GetGrainDemandListReq();
        req.setGrainTypeId(typeId);
        req.setLevelId(levelId);
        req.setBuyerId(buyerId);
        grainDataRepository.getGrainDemandList(new DefaultObserver<List<GrainDemand>>() {
            @Override
            public void onNext(List<GrainDemand> grainDemands) {
                List<GrainDemandItemViewModel> list = new ArrayList<>();
                if (grainDemands != null && !grainDemands.isEmpty()) {
                    for (GrainDemand item : grainDemands) {
                        list.add(new GrainDemandItemViewModel(item));
                    }
                }
                resetData(list);
            }

            @Override
            public void onError(MyException e) {
                setError(e.getMessage());
            }
        }, req);
    }

    @Override
    public void loadData() {
        super.loadData();
        GetGrainDemandListReq req = new GetGrainDemandListReq();
        req.setGrainTypeId(typeId);
        req.setLevelId(levelId);
        req.setBuyerId(buyerId);
        req.setDemandId(commListViewModel.getLastItem().toString());
        grainDataRepository.getGrainDemandList(new DefaultObserver<List<GrainDemand>>() {
            @Override
            public void onNext(List<GrainDemand> grainDemands) {
                List<GrainDemandItemViewModel> list = new ArrayList<>();
                if (grainDemands != null && !grainDemands.isEmpty()) {
                    for (GrainDemand item : grainDemands) {
                        list.add(new GrainDemandItemViewModel(item));
                    }
                }
                addData(list);
            }

            @Override
            public void onError(MyException e) {
                finishLoadMoreError();
            }
        }, req);
    }

}
